package com.apop.common.model;

import com.apop.common.exception.ErrorCode;
import lombok.Data;

/**
 * Result
 * Author: zjy
 * Date: 2021/11/9  15:23
 * Describe: 返回结果封装
 */
@Data
public class Result<T> {

    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> success(T data){
        Result<T> r = new Result<T>(data);
        r.code = 0;
        r.msg = "ok";
        return r;
    }

    public static <T> Result<T> fail(ErrorCode errorCode){
        Result<T> r = new Result<T>();
        r.code = errorCode.getCode();
        r.msg = errorCode.getMsg();
        return r;
    }

    /**
     * 建议用上面的重载方法 fail(ErrorCode errorCode)
     * @param code
     * @param msg
     * @param <T>
     * @return
     */


    @Deprecated
    public static <T> Result<T> error(CodeMsg codeMsg){
        Result<T> r = new Result<T>(codeMsg);
        r.code = 500;
        return r;
    }

    @Deprecated
    public static <T> Result<T> error(CodeMsg codeMsg,String htm){
        Result<T> r = new Result<T>(codeMsg);
        r.code = 500;
        r.msg=htm;
        return r;
    }
    private Result() {
        this.code = 0;
        this.msg = "";
    }

    private Result(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    private Result(CodeMsg codeMsg) {
        if(codeMsg == null) {
            return;
        }
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
    }




}
